package com.icart.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;


import org.springframework.http.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;



@Controller
public  class WxUtilController {
    public String sendMessage(String data,RestTemplate template,HttpServletRequest request) {
        /* 师兄接口的token*/
        String accesstoken=getAccesstoken(template,request);
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN";
        requestUrl = requestUrl.replace("ACCESS_TOKEN",accesstoken);
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        HttpEntity<String> entity = new HttpEntity<String>(data,headers);
        String results = template.postForObject(requestUrl, entity, String.class);
        System.out.println(results);
        return results;
    }

    /* 养护通获得师兄接口的token,放在session里面*/
    public String getToken(RestTemplate template,HttpServletRequest request) {
        if(request.getSession().getAttribute("token")==null){
            String url = "http://api.wechat.ikartek.com/ftjapp/apptoken?timestamp=1482837935001&appid=F51E98BFA290DF2A023D5EA6D8AEF818&secret=D4A249930207AC73737C1992FD19CE34&refresh=1";
            String content = template.getForEntity(url, String.class).getBody();
            JSONObject jsonObject = JSON.parseObject(content);
            String result = jsonObject.getString("result");/*获取师兄接口token,有它才可以用微信的东西，这是师兄的接口，他的接口里面有微信的接口*/
            System.out.println("token=" + result);
            request.getSession().setAttribute("token", result);
            return result;
        } else {
            String result= request.getSession().getAttribute("token").toString();
            return result;
        }
    }

    /* 获得微信接口accesstoken,放在session里面*/
    public String getAccesstoken(RestTemplate template,HttpServletRequest request) {
        if(request.getSession().getAttribute("accesstoken")==null){
            String result=getToken(template,request);
            String url2="http://pay.wechat.ikartek.com/jssdk/api.php?token="+result;
            String content2= template.getForEntity(url2,String.class).getBody();
            JSONObject jsonObject2= JSON.parseObject(content2);
            String result2 = jsonObject2.getString("result");
            JSONObject jsonObject3= JSON.parseObject(result2);
            String accesstoken = jsonObject3.getString("token");/*微信的access_token*/
            System.out.println("accesstoken="+accesstoken);
            request.getSession().setAttribute("accesstoken", accesstoken);
            return accesstoken;
        } else {
            String accesstoken= request.getSession().getAttribute("accesstoken").toString();
            return accesstoken;
        }
    }

    /* 爱卡特汽车科技获得师兄接口的token,放在session里面*/
    public String getToken2(RestTemplate template,HttpServletRequest request) {
        if(request.getSession().getAttribute("token2")==null){
            String url = "http://api.wechat.ikartek.com/ftjapp/apptoken?timestamp=1482837935001&appid=B19CF90187B1445DD2EA0EB901E1F098&secret=A04C81CA8B47EF6646B993B8E5259E44&refresh=1";
            String content = template.getForEntity(url, String.class).getBody();
            JSONObject jsonObject = JSON.parseObject(content);
            String result = jsonObject.getString("result");/*获取师兄接口token,有它才可以用微信的东西，这是师兄的接口，他的接口里面有微信的接口*/
            System.out.println("token2=" + result);
            request.getSession().setAttribute("token2", result);
            return result;
        } else {
            String result= request.getSession().getAttribute("token2").toString();
            return result;
        }
    }

    /* 爱卡特汽车科技获得师兄接口的token,放在session里面*/
    public String getAccesstoken2(RestTemplate template,HttpServletRequest request) {
        if(request.getSession().getAttribute("accesstoken2")==null){
            String result=getToken2(template,request);
            String url2="http://pay.wechat.ikartek.com/jssdk/api.php?token="+result;
            String content2= template.getForEntity(url2,String.class).getBody();
            JSONObject jsonObject2= JSON.parseObject(content2);
            String result2 = jsonObject2.getString("result");
            JSONObject jsonObject3= JSON.parseObject(result2);
            String accesstoken = jsonObject3.getString("token");/*微信的access_token*/
            System.out.println("accesstoken2="+accesstoken);
            request.getSession().setAttribute("accesstoken2", accesstoken);
            return accesstoken;
        } else {
            String accesstoken= request.getSession().getAttribute("accesstoken2").toString();
            return accesstoken;
        }
    }

    //获得微信头像和名字
    public Object getUser(RestTemplate template,HttpServletRequest request,String openid) {
        /* 师兄接口的token*/
        if(request.getSession().getAttribute("wxuser")==null){
        String accesstoken=getAccesstoken(template,request);
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";
        requestUrl = requestUrl.replace("ACCESS_TOKEN",accesstoken).replace("OPENID",openid);
        Object results = template.getForObject(requestUrl,Object.class);
        System.out.println("results="+results);
        request.getSession().setAttribute("wxuser", results);
        return results;
        } else {
            Object results= request.getSession().getAttribute("wxuser");
            return results;
        }
    }

}